package com.hr.reactive.mono.s02;

import com.hr.reactive.common.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author Administrator
 * 延迟发布者的创建
 */
public class Lec09FromDefer {
    private static final Logger log = LoggerFactory.getLogger(Lec09FromDefer.class);

    public static void main(String[] args) {
      //  createPublisher().subscribe(Utils.subscriber());
        //defer延迟创建发布者
        Mono.defer(()->createPublisher()).subscribe(Utils.subscriber());
    }

    private static Mono<Integer> createPublisher(){

        Utils.sleepSeconds(1000);
        log.info("createPublisher ");
        var list = List.of(1, 2, 3);
        return Mono.fromSupplier(()->sum(list));
    }
    private static int sum(List<Integer> list) {
        Utils.sleepSeconds(3000);
        log.info("finding the sum of {}", list);
        return list.stream().mapToInt(a -> a).sum();
    }


}


